package ute.mvc.controller.portal.lecturer;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ute.mvc.dto.ClassScoreDTO;
import ute.mvc.model.MyClass;
import ute.mvc.model.TestAndQuizz;
import ute.mvc.service.ClassService;
import ute.mvc.service.TestAndQuizzService;
import ute.mvc.util.JsonObject;

@Controller
@RequestMapping(value = "/lecturer/{lecturerId}/{openCourseId}/{subjectId}/{testId}/{classId}/")
public class ReportController {
	
	@Autowired
	ClassService classService;
	
	@Autowired
	TestAndQuizzService testService;

	@RequestMapping(method = RequestMethod.GET)
	public String getReport(@PathVariable String testId,
			@PathVariable String classId, Model model) {

		MyClass mClass = classService.getClassById(classId);
		TestAndQuizz test = testService.getTestAndQuizz(testId);
		// Just return the report page
		model.addAttribute("mClass", mClass);
		model.addAttribute("test", test);
		return "test_report";
	}

	/**
	 * 
	 * @param testId
	 * @param classId
	 * @return a json string for chart.js show chart
	 */
	
	@RequestMapping(value = "/chartData", method = RequestMethod.GET)
	@ResponseBody
	public String getChart(@PathVariable String testId,
			@PathVariable String classId) {

		return "chart_string";
	}

	/**
	 * 
	 * @param testId
	 * @param classId
	 * @return best score for each student in class if student do not do this
	 *         test, score is 0
	 */
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/studentData", method = RequestMethod.GET)
	@ResponseBody
	public JsonObject getStudentMark(@PathVariable String testId,
			@PathVariable String classId) {

		List<ClassScoreDTO> lstScore = testService.getScoreByClass(testId, classId);
		
		JsonObject testJson = new JsonObject();
		testJson.setAaData(lstScore);
		testJson.setiTotalRecords(lstScore.size());
		testJson.setiTotalDisplayRecords(lstScore.size());
		testJson.setsEcho(1);
		return testJson;
	}

}
